package com.xoa.controller.workPlan;


import com.xoa.model.users.Users;
import com.xoa.model.workPlan.WorkPlanWithBLOBs;
import com.xoa.service.workPlan.WorkPlanService;
import com.xoa.util.CookiesUtil;
import com.xoa.util.DateCompute;
import com.xoa.util.DateFormat;
import com.xoa.util.ToJson;
import com.xoa.util.common.http.Request;
import com.xoa.util.common.session.SessionUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

/**
 * Created by gsb on 2017/9/5.
 */
@Controller
@RequestMapping("/workPlan")
public class WorkPlanController {
    @Resource
    private WorkPlanService workPlanService;


    @RequestMapping("/index")
    public String index(){
        return "app/workPlan/index";
    }

    /**
     * 工作计划管理
     * @return
     */
    @RequestMapping("/workPlanManager")
    public String workPlanManager(){
        return "app/workPlan/workPlanManager";
    }

    /**
     * 工作计划编辑（editFlag=0 新建  editFlag=1 编辑）
     * @param editFlag
     * @param map
     * @return
     */
    @RequestMapping("/workPlanEdit")
    public String workPlanEdit(int editFlag,int planId, Map<String,Object> map,HttpServletRequest request){
        map.put("editFlag",editFlag);
        map.put("planId",planId);
        return "app/workPlan/workPlanEdit";
    }

    /**
     * 工作管理中的工作计划查询
     * @return
     */

    @RequestMapping("/workPlanQuery")
    public String workPlanQuery(){
        return "app/workPlan/workPlanQuery";
    }

    /**
     * 工作计划查询
     * @return
     */

    @RequestMapping("/workPlanQueryIndex")
    public String workPlanQueryIndex(){
        return "app/workPlan/workPlanQueryIndex";
    }

    /**
     * 工作计划类型设置
     * @return
     */

    @RequestMapping("/workPlanTypeSetting")
    public String workPlanTypeSetting(){
        return "app/workPlan/workPlanTypeSetting";
    }

    /**
     * 工作计划查询（time-1：今日，time-2：本周，time-3：本月）
     * @return
     */

    @RequestMapping("/workPlanQuList")
    public String workPlanQuList(int time, Map<String,Object> map){
        map.put("time",time);
        return "app/workPlan/workPlanQuList";
    }

    /**
     * 工作计划查询中的查询
     * @return
     */

    @RequestMapping("/workPlanQuQuery")
    public String workPlanQuQuery(){
        return "app/workPlan/workPlanQuQuery";
    }

    /**
     * 创建作者:   牛江丽
     * 创建日期:   2017年9月8日 下午16:38:00
     * 方法介绍:   根据条件查询工作计划
     * 参数说明:   @param eduFixAssets
     * 返回值说明:
     */
    @ResponseBody
    @RequestMapping("/selectWorkPlanByCond")
    ToJson<WorkPlanWithBLOBs> selectWorkPlanByCond( @RequestParam(value="output", required = false,defaultValue = "0") int output,
                                                    WorkPlanWithBLOBs workPlanWithBLOBs, HttpServletRequest request, HttpServletResponse response, HttpSession session, Integer page, Integer pageSize, boolean useFlag){
        try{
            String [] weekArray= DateCompute.getWeekInterval().split(",");
            String [] monthArray=DateCompute.getMonthInterval().split(",");
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
            Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
            Users user= SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(),redisSessionCookie);
            if(workPlanWithBLOBs!=null){
                workPlanWithBLOBs.setCurrentUser(String.valueOf(user.getUid()));
                if(workPlanWithBLOBs.getTime()==1){
                    Date date1=new Date();
                    workPlanWithBLOBs.setBeginDate(sdf.parse(sdf.format(date1)));
                    workPlanWithBLOBs.setEndDate(sdf.parse(sdf.format(date1)));
                }else if(workPlanWithBLOBs.getTime()==2){
                    workPlanWithBLOBs.setBeginDate(sdf.parse(weekArray[0]));
                    workPlanWithBLOBs.setEndDate(sdf.parse(weekArray[1]));
                }else if(workPlanWithBLOBs.getTime()==3){
                    workPlanWithBLOBs.setBeginDate(sdf.parse(monthArray[0]));
                    workPlanWithBLOBs.setEndDate(sdf.parse(monthArray[1]));
                }
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return workPlanService.selectWorkPlanByCond(output,workPlanWithBLOBs,request,response,session,page,pageSize,useFlag);
    }

    /**
     * 创建作者:   牛江丽
     * 创建日期:   2017年9月8日 下午16:48:00
     * 方法介绍:   添加工作计划
     * 参数说明:   @param eduFixAssets
     * 返回值说明:
     */
    @ResponseBody
    @RequestMapping("/insertWorkPlan")
    ToJson<WorkPlanWithBLOBs> insertWorkPlan(WorkPlanWithBLOBs workPlanWithBLOBs,HttpServletRequest request){
        return workPlanService.insertWorkPlan(workPlanWithBLOBs,request);
    }


    /**
     * 创建作者:   牛江丽
     * 创建日期:   2017年9月8日 下午16:48:00
     * 方法介绍:   修改工作计划
     * 参数说明:   @param eduFixAssets
     * 返回值说明:
     */
    @ResponseBody
    @RequestMapping("/updateWorkPlanById")
    ToJson<WorkPlanWithBLOBs> updateWorkPlanById(HttpServletRequest request,WorkPlanWithBLOBs workPlanWithBLOBs){
        return workPlanService.updateWorkPlanById(request,workPlanWithBLOBs);
    }

    /**
     * 创建作者:   牛江丽
     * 创建日期:   2017年9月8日 下午17:32:00
     * 方法介绍:   根据id删除工作计划
     * 参数说明:   @param id
     * 返回值说明:
     */
    @ResponseBody
    @RequestMapping("/delWorkPlanById")
    ToJson<WorkPlanWithBLOBs> delWorkPlanById(int planId){
        return workPlanService.delWorkPlanById(planId);
    }

    /**
     * 创建作者:   牛江丽
     * 创建日期:   2017年9月8日 下午17:32:00
     * 方法介绍:   根据id字符串删除工作计划（多项删除，逗号分隔）
     * 参数说明:   @param id
     * 返回值说明:
     */
    @ResponseBody
    @RequestMapping("/deleteWorkPlanByIds")
    public ToJson<WorkPlanWithBLOBs> deleteWorkPlanByIds(String planIds) {
        return workPlanService.deleteWorkPlanByIds(planIds);
    }

    /**
     * 创建作者:   牛江丽
     * 创建日期:   2017年9月11日 下午14:18:00
     * 方法介绍:   根据id进行查询工作计划
     * 参数说明:   @param id 资产id
     * 返回值说明:
     */
    @ResponseBody
    @RequestMapping("/selectWorkPlanById")
    ToJson<WorkPlanWithBLOBs> selectWorkPlanById(int planId,HttpServletRequest request){
        return workPlanService.selectWorkPlanById(planId, request);
    }
    //编辑,生效,结束接口
    @ResponseBody
    @RequestMapping("/updatePublish")
    public ToJson<WorkPlanWithBLOBs> updatePublish(WorkPlanWithBLOBs workPlanWithBLOBs){
        return workPlanService.updatePublish ( workPlanWithBLOBs );
    }

}
